package cn.jsl.tran;

import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

/**
 * 自定义事务监听器
 */
public class MyTranListenter implements TransactionListener {
    @Override
    public LocalTransactionState executeLocalTransaction(Message message, Object o) {
        //将消息保存到本地，防止发送失败丢失
        System.out.println("执行本地事务提交！");
        System.out.println("接收到的参数"+ o);
        return LocalTransactionState.COMMIT_MESSAGE;//本地事务执行成功，确认提交
    }

    /**
     * 检查本地事务状态
     * @param messageExt
     * @return
     */
    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
        //检查本地事务成功没有，如果成功了，主动告诉MQ 我本地成功了，MQ可以真的发送事务消息成功了
        System.out.println("校验本地事务是否成功！");
        return LocalTransactionState.COMMIT_MESSAGE;
    }
}
